package com.trip.service;

import com.trip.dao.tables.pojos.PicDomain;
import com.trip.dao.tables.pojos.StateDomain;
import com.trip.dao.tables.pojos.TagDomain;
import com.trip.dao.tables.records.TagRecord;
import org.jooq.DSLContext;
import org.jooq.SelectConditionStep;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

import static com.trip.dao.Tables.*;

/**
 * Created by hc24 on 2017/10/1.
 */
@Service
@Transactional(readOnly = true)
public class BasicInfoService {

    @Autowired
    private DSLContext dslContext;

    public PicDomain queryPicById(String id){
        return dslContext.selectFrom(PIC)
                .where(PIC.ID.eq(id))
                .fetchOne().into(PicDomain.class);
    }

    public List<StateDomain> queryStateList(){
        return dslContext.selectFrom(STATE)
                .fetchStream().map(e->e.into(StateDomain.class)).collect(Collectors.toList());
    }

    public List<TagDomain> queryTagList(Integer type){
        SelectConditionStep<TagRecord> scs=dslContext.selectFrom(TAG).where("1=1");
                if(type!=null)
                    scs=scs.and(TAG.TAG_TYPE.eq(type));
         return scs.fetchStream().map(e->e.into(TagDomain.class)).collect(Collectors.toList());
    }

}
